#ifndef _MASSSTOR_H
#define _MASSSTOR_H     1
#include"usb.h"

#define DATAIN    0x80
#define DATAOUT 0x00
typedef 
struct  MassTrans
{
       BYTE *  CmdPtr;
	BYTE *  DataPtr;
	BYTE     bDirection; // bit 7:    1  --in; 0 --out
	DWORD dDataSize;
	BYTE     bCmdSize; //whole cmd size
//	WORD   wPreSize;  //usage not clear
//	WORD   wPostSize;	
	
}MassTransType;


typedef
struct MassStorage{
	BYTE PDT;
	BYTE VendorID[9];
	BYTE ProductID[17];
	BYTE ProductVL[5];
	DWORD LastLBA;
	DWORD BlockLength;
	BYTE      bLUN;//logical units number
}MassStorageType;

//BULK ONLY TRANSFER.

//Command Block Wrapper
#define CBWSignature 0x43425355
#define CSWSignature 0x53425355
typedef  struct CBW  CBWType;
struct CBW
{
       DWORD   dCBWSignature;                      //43425355h
	DWORD   dCBWTag;
	DWORD   dCBWDataTransferLength;
	BYTE        bmCBWFlags;
	BYTE        bCBWLUN;
	BYTE        bCBWCBLength;
	BYTE        CBWCB[16];
};


typedef struct CSW CSWType;
struct CSW
{
        DWORD  dCSWSignature;
	DWORD  dCSWTag;
	DWORD  dCSWDataResidue;
	BYTE       bCSWStatus;
};



////////////         SCSI Primary Commads (SPC-2)definition  area

typedef struct CDB6Byte  CDB6ByteType;
struct CDB6Byte
{
         BYTE  OPTCode; //byte 0
         BYTE  LBA1 ;        //byte 1
         BYTE  LBA2 ;       //byte 2
         BYTE  LBA3;      //byte 3
         BYTE  Length;   //byte 4
         BYTE  Control; //byte 5
};


typedef struct CDB10Byte  CDB10ByteType;
struct CDB10Byte
{
         BYTE  OPTCode; //byte 0
         BYTE  ServiceAction;
         DWORD  LBA ;        //4//4  bytes
	  BYTE  Reserved;	 
         WORD  Length;   //
         BYTE  Control; //
};


typedef struct CDB12Byte  CDB12ByteType;
struct CDB12Byte
{
         BYTE  OPTCode; //byte 0
         BYTE  ServiceAction;
         DWORD  LBA ;        //4//4  bytes
         DWORD  Length;   //
	  BYTE  Reserved;	          
         BYTE  Control; //
};

typedef struct CDB16Byte  CDB16ByteType;
struct CDB16Byte
{
         BYTE  OPTCode; //byte 0
         BYTE  ServiceAction;
         DWORD  LBA ;        //4//4  bytes
         DWORD  AdditionCDB;
         DWORD  Length;   //
	  BYTE  Reserved;	          
         BYTE  Control; //
};


//require SCSI commands
#define TESTUnitReady 0x00
#define REQUESTSense  0x03
#define INQUIRY            0x12
#define READCAPACITY 0x25
#define READ10             0x28

//Peripheral device type 

#define SBCType 0x00
#define SSCSType 0x01
#define SSCpType 0x02
#define SPC2Type 0x03
#define SBCWriteOnce 0x04
#define MMC2Type 0x05
#define SCSI2SType 0x06
#define SBCOType 0x07
#define SMCType 0x08
#define SCSI2CType 0x08
#define SCC2Type 0x0c
#define SECType 0x0d
#define RBCType 0x0e
#define OCRWType 0x0f
#define OSDType 0x11


//declaration 

/******************************************************************************
    Function:
        BOTTransation

    Description:
	process a BULK-ONLY Transport.

    Entry:
	MTPtr  -- command  data structure
	LengthPtr -- actual data transport length
	DevPtr -- Transport on the pointer device information  

    Exit:
        none
******************************************************************************/
void  BOTTransation(MassTransType * MTPtr, WORD * LengthPtr,DeviceInfoPtr DevPtr );

/******************************************************************************
    Function:
        BOTCBW

    Description:
	send CBW(Command Block Wrapper) on BULK transfer.

    Entry:
	MTPtr  -- command  data structure
	 CmdPtr  --- point to CBWType, shall set CBWCB, other field not.
                 DataPtr --- if have data to send or receive, point to data
                 bDirection --- bit 7: 1---in; 0---out
                 dDataSize --- size of data between CBW and CSW
                 bCmdSize --- size of CBW
	DevPtr -- Transport on the pointer device information  
	
    Exit:
       status:  0--ok
                   bit0: STALL
                   bit1: time out
                   bit2: error
******************************************************************************/
BYTE BOTCBW(MassTransType * MTPtr, DeviceInfoPtr DevPtr );

/******************************************************************************
    Function:
        BOTData

    Description:
	data phase on BULK transfer.

    Entry:
	MTPtr  -- command  data structure
	 CmdPtr  --- point to CBWType, shall set CBWCB, other field not.
                 DataPtr --- if have data to send or receive, point to data
                 bDirection --- bit 7: 1---in; 0---out
                 dDataSize --- size of data between CBW and CSW
                 bCmdSize --- size of CBW
	DevPtr -- Transport on the pointer device information  
	
    Exit:
       status:  0--ok
                   bit0: STALL
                   bit1: time out
                   bit2: error
******************************************************************************/
BYTE BOTData(MassTransType * MTPtr,WORD * LengthPtr,DeviceInfoPtr DIPtr);

/******************************************************************************
    Function:
        BOTCSW

    Description:
	send CSW(Command Status Wrapper) on BULK transfer.

    Entry:
	MTPtr  -- command  data structure
	 CmdPtr  --- point to CBWType, shall set CBWCB, other field not.
                 DataPtr --- if have data to send or receive, point to data
                 bDirection --- bit 7: 1---in; 0---out
                 dDataSize --- size of data between CBW and CSW
                 bCmdSize --- size of CBW
	DevPtr -- Transport on the pointer device information  
	
    Exit:
    status:
      00---command pass
      01---command failed
      02---phase error
      03---bulk get CSW error. may be STALL, time out, error.

      note: receive CSW in MassTransType.CmdPtr
******************************************************************************/
BYTE BOTCSW(MassTransType * MTPtr,DeviceInfoPtr DIPtr);

/******************************************************************************
    Function:
        BOTReset

    Description:
	reset bulk endpoint.

    Entry:
	DevPtr -- Transport on the pointer device information  
	
    Exit:
	none

******************************************************************************/
void BOTReset(DeviceInfoPtr DIPtr);

/******************************************************************************
    Function:
     clearBulkEndpoint

    Description:
	set CLEAR FEATURE on bulk endpointer.

    Entry:
       direction  -- bit7=0    OUT bulk; bit7=1    IN bulk
	DevPtr -- Transport on the pointer device information  
	
    Exit:
	none

******************************************************************************/
void clearBulkEndpoint(BYTE direction,DeviceInfoPtr DIPtr);



/******************************************************************************
    Function:
        MassTransation

    Description:
	process a mass storage transport.

    Entry:
	MTPtr  -- command  data structure
	LengthPtr -- actual data transport length
	DevPtr -- Transport on the pointer device information  

    Exit:
        none
******************************************************************************/
void  MassTransation(MassTransType * MTPtr, WORD * LengthPtr,DeviceInfoPtr DevPtr);


/******************************************************************************
    Function:
        MassINQUIRY

    Description:
	 send a INQUIRY command to storage device.

    Entry:
	DataPtr  --data buffer pointer to INQUIRY data	
	DevPtr -- Transport on the pointer device information  

    Exit:
        none
******************************************************************************/
void MassINQUIRY(BYTE * DataPtr,DeviceInfoPtr DevPtr);

/******************************************************************************
    Function:
        MassRequestSense

    Description:
	 The REQUEST SENSE command instructs the Device to transfer sense data to the host computer.

    Entry:
	DevPtr -- Transport on the pointer to device information  

    Exit:
        SenseKey--The Sense Key provides generic categories in which error and exception conditions 
        can be reported.
	 BIT 16:23 :ADDITIONAL SENSE CODE QUALIFIER
        BIT  8:15: ADDITIONAL SENSE CODE
        BIT 0:7:    SENSE KEY
******************************************************************************/
DWORD MassRequestSense(DeviceInfoPtr DevPtr);

/******************************************************************************
    Function:
        MassDevReady

    Description:
	 The TEST UNIT READY command provides a means to check if the Device is ready.

    Entry:
	DevPtr -- Transport on the pointer to device information  

    Exit:
	none
******************************************************************************/
void MassDevReady(DeviceInfoPtr DevPtr);

/******************************************************************************
    Function:
        MassTestUnitReady

    Description:
	 The TEST UNIT READY command provides a means to check if the Device is ready.

    Entry:
	DevPtr -- Transport on the pointer to device information  

    Exit:
        SenseKey--The Sense Key provides generic categories in which error and exception conditions 
        can be reported.
	 BIT 16:23 :ADDITIONAL SENSE CODE QUALIFIER
        BIT  8:15: ADDITIONAL SENSE CODE
        BIT 0:7:    SENSE KEY
******************************************************************************/
DWORD MassTestUnitReady(DeviceInfoPtr DevPtr);


/******************************************************************************
    Function:
        MassREAD10

    Description:
	 The READ(10) command requests that the Device transfer data to the host computer.

    Entry:
       LBA --logical block address
       BlockLength --specifies the number of contiguous logical blocks of data that are transferred in bytes
       DataPtr -- getted data buffer 
	DevPtr -- Transport on the pointer to device information  

    Exit:
	none
******************************************************************************/
void MassREAD10(DWORD LBA,	WORD BlockLength,BYTE * DataPtr,DeviceInfoPtr DevPtr);

/******************************************************************************
    Function:
        MassREADCapacity

    Description:
	 The READ CAPACITY command provides a means for the host computer to request information regarding
	the capacity of the installed medium of the device..

    Entry:
       DataPtr -- getted data buffer 
	DevPtr -- Transport on the pointer to device information  

    Exit:
	none
******************************************************************************/
void MassREADCapacity(BYTE * DataPtr,DeviceInfoPtr DevPtr);

/******************************************************************************
    Function:
        MassFORMAT

    Description:
	The host sends the FORMAT UNIT command to physically format a diskette according to selected options.

    Entry:      
	DevPtr -- Transport on the pointer to device information  

    Exit:
	none
******************************************************************************/
void MassFORMAT(DeviceInfoPtr DevPtr);

/******************************************************************************
    Function:
        MassWRITE10

    Description:
	The WRITE(10) command requests that the Device write the data transferred by the Host Computer to the
	medium.

    Entry:      
       LBA --logical block address
       BlockLength --specifies the number of contiguous logical blocks of data that are transferred in bytes
       DataPtr --  data buffer to be write. 
	DevPtr -- Transport on the pointer to device information  

    Exit:
	none
******************************************************************************/
void MassWRITE10(DWORD LBA,	WORD BlockLength,BYTE * DataPtr,DeviceInfoPtr DevPtr);

void MassInitDev(void * ptr);
DevDriverMethodType * getMassDevMethod();

#endif